var Mock = require("mockjs");

export const getRiskData = () => {
  const unitArray = [
    "广东分公司",
    "深圳分公司",
    "云南分公司",
    "重庆分公司",
    "安徽分公司",
    "福建分公司",
    "上海分公司",
    "山东分公司",
  ];
  const data = unitArray.map((item) => {
    const dataItem = Mock.mock({
      id: "@increment",
      unitName: item,
      riskCount: "@integer(300, 5000)",
      money: "@integer(300, 5000)",
    });
    return dataItem;
  });

  return data;
};
export const getDisasterData = () => {
  const data = [
    { id: 0, name: "暴雨", count: 15 },
    { id: 0, name: "台风", count: 3 },
    { id: 0, name: "大雾", count: 6 },
    { id: 0, name: "洪涝", count: 4 },
    { id: 0, name: "大雪", count: 2 },
    { id: 0, name: "冰雹", count: 7 },
  ];

  return data;
};
export const getEventData = () => {
  const unitArray = [
    "广东分公司",
    "深圳分公司",
    "云南分公司",
    "重庆分公司",
    "安徽分公司",
    "福建分公司",
    "上海分公司",
    "山东分公司",
  ];
  const data = unitArray.map((item) => {
    const dataItem = Mock.mock({
      id: "@increment",
      createStartTime: "@datetime",
      createEndTime: "@datetime",
      emerAreaName: "@first",
      unitName: item,
      "abnormalWeather|1": ["暴雨", "台风", "大雾", "洪涝", "大雪", "冰雹"],
      "realRiskRank|1": ["一级", "二级", "三级", "四级", "五级"],
      startTime: "@now('yyyy-MM-dd')",
      subjectCount: "@integer(300, 5000)",
    });
    return dataItem;
  });

  return data;
};

const hexToRgb = (hex) => {
  var r = parseInt(hex.slice(1, 3), 16);
  var g = parseInt(hex.slice(3, 5), 16);
  var b = parseInt(hex.slice(5, 7), 16);
  // return [r, g, b];
  return `${r},${g},${b}`;
};

export const addKeyFrames = (name, color) => {
  const rgb = hexToRgb(color);
  const keyFrames = `
    @keyframes ${name} {
        0% {
            box-shadow:
                0 0 0 0 rgba(${rgb}, 0.3),
                0 0 0 15px rgba(${rgb}, 0.3),
                0 0 0 30px rgba(${rgb}, 0.3);
        }
    
        100% {
            box-shadow:
                0 0 0 15px rgba(${rgb}, 0.3),
                0 0 0 30px rgba(${rgb}, 0.3),
                0 0 0 50px rgba(${rgb}, 0);
        }
    }
  `;
  const style = document.createElement("style");
  style.innerHTML = keyFrames;
  document.querySelector("head").appendChild(style);
};

//  警戒线坐标数据 
const earlyWarnLine = [
  {
    key: 24,
    name: "小时警戒线",
    color: "red",
    list: [
      [105, 0],
      [113, 4.5],
      [119, 11],
      [119, 18],
      [127, 22],
      [127, 34],
    ],
  },
  {
    key: 48,
    name: "小时警戒线",
    color: "yellow",
    list: [
      [105, 0],
      [120, 0],
      [132, 15],
      [132, 34],
    ],
  },
];

export const drawGuardLine = (map) => {
  earlyWarnLine.forEach(item => {
      //  以坐标点划线
      let list = item.list;
      let polylinepts = [];
      let listLength = list.length;
      let polylineOptions = {
          strokeColor: item.color, 
          strokeWeight: 2, 
          strokeOpacity: 0.3
      }

      list.forEach(each => {
        //	画地图所需的点实例
          polylinepts.push(new BMapGL.Point(...each));
      });
      //	将点实例做成线实例
      const polyline = new BMapGL.Polyline(polylinepts, polylineOptions);
      polyline.enableMassClear();	//	允许清除线
      map.addOverlay(polyline);	//	将生成的线实例画到地图上

      // 警戒线名称也要画到地图
      let html = item.name;
      html = item.key + "<br>" + html.split('').join("<br>");
      let label = new BMapGL.Label(html, {
          position: polylinepts[polylinepts.length - 1],
          offset: new BMapGL.Size(0, 50)
      })
      label.setStyle({
          color: item.color,
          textAlign: 'center',
          border: "none",
          fontSize: "12px",
          backgroundColor: "transparent",
          opacity: 0.9
      });
      map.addOverlay(label);
  });
}